﻿using Alos.IRepository;
using Alos.IService;
using PetaPoco;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Alos.Entity;
using Alos.Common;
using System.Threading.Tasks;

namespace Alos.Service
{
    public class ReLessionService : BaseService<ReLession>, IReLessionService
    {
        IReLessionRepository ReLessionRepository;
        IReResourceRepository ReResourceRepository;
        public ReLessionService(IReLessionRepository _ReLessionRepository,
                                IReResourceRepository _ReResourceRepository)
        {
            ReLessionRepository = _ReLessionRepository;
            ReResourceRepository = _ReResourceRepository;
        }
        public async Task<List<dynamic>> GetStepByLessionID(int id)
        {
            var _steps = ReLessionRepository.GetStepByLessionID(id).ToList();

            if (_steps == null || _steps.Count <= 0)
                return _steps;

            //步骤编号
            string stepids = string.Empty;
            string PPTResourceID = "0";

            _steps.ForEach((_step) =>
            {
                stepids += _step.StepID + ",";
                PPTResourceID += _step.PPTResourceID + ",";
            });

            if (!string.IsNullOrWhiteSpace(stepids.Trim(',')))
            {
                var PptInfo = await Task.Factory.StartNew(pptIds =>
                {
                    return ReResourceRepository.GetReResourceById(pptIds.ToString()).ToList();
                }, PPTResourceID.Trim(','));

                var ResInfo = await Task.Factory.StartNew(stepIds =>
                {
                    return ReResourceRepository.GetReResourceByStepId(stepIds.ToString()).ToList();
                }, stepids.Trim(','));

                if (PptInfo != null && PptInfo.Count > 0)
                {
                    PptInfo.ForEach(ppt =>
                    {
                        ResInfo.Add(new
                        {
                            LessonID = _steps[0].LessonID,
                            StepID = _steps[0].StepID,
                            ResourceID = ppt.ID,
                            ReOrginName = ppt.ReOrginName,
                            ResourceFormat = ppt.ResourceFormat,
                            ReTypeCode = ppt.ReTypeCode,
                            ResourceName = ppt.ResourceName,
                            TechType = ppt.TechType,
                            QtypeName = ppt.QtypeName
                        });
                    });
                }
                _steps.ForEach(_stem =>
                {
                    _stem.Resources = ResInfo.FindAll(res => res.StepID.ToString() == _stem.StepID.ToString());
                });
            }
            return _steps;
        }

        public IEnumerable<dynamic> GetReLessionClassPeriodByChapterID(int cid, int uid)
        {
            return ReLessionRepository.GetReLessionClassPeriodByChapterID(cid, uid);
        }
    }
}
